package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysWxRole;
import com.hzhc.entity.SysWxRoleMenu;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.SysWxRoleMapper;
import com.hzhc.mapper.SysWxRoleMenuMapper;
import com.hzhc.service.SysWxRoleMenuService;
import com.hzhc.service.SysWxRoleService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysWxRoleVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 微信小程序角色管理 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2023-11-07
 */
@Service
@RequiredArgsConstructor
public class SysWxRoleServiceImpl extends ServiceImpl<SysWxRoleMapper, SysWxRole> implements SysWxRoleService {
    private final SysWxRoleMenuMapper sysWxRoleMenuMapper;

    private final SysWxRoleMenuService sysWxRoleMenuService;

    @Override
    public List<SysWxRole> getList(Page<SysWxRole> pagination, SysWxRoleVo vo) {
        Page<SysWxRole> page = baseMapper.selectPage(pagination, Wrappers.<SysWxRole>lambdaQuery()
                .like(StrUtil.isNotBlank(vo.getName()), SysWxRole::getName, vo.getName()));
        return page.getRecords();
    }

    @Override
    public ResultUtil<T> addRet(HttpServletRequest request, SysWxRole sysWxRole) {
        boolean b = save(sysWxRole);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public  ResultUtil<T> updateDelete(Long id) {
        int i = baseMapper.deleteById(id);
        if(i>0){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public  ResultUtil<T> updateRet(HttpServletRequest request, SysWxRole sysWxRole) {
        boolean b = updateById(sysWxRole);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> checkWxName(SysWxRoleVo data) {
        QueryWrapper<SysWxRole> queryWrapper = new QueryWrapper<>();
        if(data.getId()!=null){
            queryWrapper.ne("id",data.getId());
        }
        queryWrapper.eq("name",data.getName());
        SysWxRole sysWxRole = baseMapper.selectOne(queryWrapper);
        if(sysWxRole!=null){
            return ResultUtil.build(ResultUtilCodeEnum.WX_NAME_EXIST);
        }else{
            return ResultUtil.ok();
        }
    }

    @Override
    public ResultUtil<T> updateGrantRoleRet(Long roleid, String data) {
        sysWxRoleMenuMapper.delete(Wrappers.<SysWxRoleMenu>lambdaQuery().eq(SysWxRoleMenu::getWxRoleId, roleid));

        List<SysWxRoleMenu> list = new ArrayList<>();
        String[] split = data.split(",");
        for (String s : split) {
            s = s.trim();
            if (StrUtil.isNotBlank(s)) {
                SysWxRoleMenu roleMenu = new SysWxRoleMenu();
                roleMenu.setWxMenuId(Long.parseLong(s));
                roleMenu.setWxRoleId(roleid);
                list.add(roleMenu);
            }
        }
        if (list.size() > 0) {
            sysWxRoleMenuService.saveBatch(list);
        }
        return ResultUtil.ok();
    }


    @Override
    public List<SysWxRole> getSysWxRoleJson(Page<SysWxRole> page, SysWxRoleVo vo) {
        IPage<SysWxRole> iPage = baseMapper.selectWxRoleJsonPage(page, vo);
        return iPage.getRecords();
    }

    @Override
    public List<Long> getWxUserMenuList(Long id) {
        return baseMapper.selectRoleMenuList(id);
    }
}
